package routers

import (
	"encoding/json"
	"github.com/beego/beego/v2/server/web/context"
	"personal-skill/util/jwtutil"
	"personal-skill/util/resultutil"
)

func init() {
	//web.InsertFilter(apiPrefix + "/*", web.BeforeExec, ApiFilter)
}

var ApiFilter = func(ctx *context.Context) {
	token := ctx.Input.Header("auth-token")
	if token == "" {
		response := resultutil.Error(resultutil.UserNotLoginErr)
		respJson, _ := json.Marshal(response)
		_, _ = ctx.ResponseWriter.Write(respJson)
		return
	}
	userInfo := jwtutil.ParseJwtToken(token)
	if userInfo == nil {
		response := resultutil.Error(resultutil.UserLoginExpireErr)
		respJson, _ := json.Marshal(response)
		_, _ = ctx.ResponseWriter.Write(respJson)
	}
	// TODO 用户权限校验
}
